package com.xydtech.ecard.config.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.ecard.config.domain.EcardModuleInfo;
import com.xydtech.ecard.config.service.IEcardModuleInfoService;
import com.xydtech.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电子名片模块功能开关配置Controller
 *
 * @author yuzhiguo
 * @date 2024-10-18 下午2:47
 */
@Controller
@RequestMapping("/ecard/config/module")
public class EcardModuleInfoController extends BaseController
{
    private String prefix = "ecard/config/module";

    @Autowired
    private IEcardModuleInfoService ecardModuleInfoService;
    @Autowired
    private ISysDeptService sysDeptService;

    @RequiresPermissions("ecard:config:module:view")
    @GetMapping()
    public String module()
    {
        return prefix + "/module";
    }

    /**
     * 查询电子名片模块功能开关配置列表
     */
    @RequiresPermissions("ecard:config:module:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardModuleInfo ecardModuleInfo)
    {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        ecardModuleInfo.setDictDeptId(sysDept.getDeptId());
        startPage();
        List<EcardModuleInfo> list = ecardModuleInfoService.selectEcardModuleInfoList(ecardModuleInfo);
        return getDataTable(list);
    }

    /**
     * 导出电子名片模块功能开关配置列表
     */
    @RequiresPermissions("ecard:config:module:export")
    @Log(title = "电子名片模块功能开关配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EcardModuleInfo ecardModuleInfo)
    {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        ecardModuleInfo.setDictDeptId(sysDept.getDeptId());
        List<EcardModuleInfo> list = ecardModuleInfoService.selectEcardModuleInfoList(ecardModuleInfo);
        ExcelUtil<EcardModuleInfo> util = new ExcelUtil<EcardModuleInfo>(EcardModuleInfo.class);
        return util.exportExcel(list, "电子名片模块功能开关配置数据");
    }

    /**
     * 新增电子名片模块功能开关配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存电子名片模块功能开关配置
     */
    @RequiresPermissions("ecard:config:module:add")
    @Log(title = "电子名片模块功能开关配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EcardModuleInfo ecardModuleInfo)
    {
        EcardModuleInfo moduleInfo = new EcardModuleInfo();
        moduleInfo.setModuleCode(ecardModuleInfo.getModuleCode());
        moduleInfo.setDeptId(ecardModuleInfo.getDeptId());
        List<EcardModuleInfo> list = ecardModuleInfoService.selectEcardModuleInfoList(moduleInfo);
        if (list.size() > 0) {
            return error("该机构已有相同的模块配置，不可以重复配置！");
        }
        ecardModuleInfo.setCreatorId(ShiroUtils.getSysUser().getUserId());
        return toAjax(ecardModuleInfoService.insertEcardModuleInfo(ecardModuleInfo));
    }

    /**
     * 修改电子名片模块功能开关配置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        EcardModuleInfo ecardModuleInfo = ecardModuleInfoService.selectEcardModuleInfoById(id);
        mmap.put("ecardModuleInfo", ecardModuleInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存电子名片模块功能开关配置
     */
    @RequiresPermissions("ecard:config:module:edit")
    @Log(title = "电子名片模块功能开关配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EcardModuleInfo ecardModuleInfo)
    {
        return toAjax(ecardModuleInfoService.updateEcardModuleInfo(ecardModuleInfo));
    }

    /**
     * 删除电子名片模块功能开关配置
     */
    @RequiresPermissions("ecard:config:module:remove")
    @Log(title = "电子名片模块功能开关配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ecardModuleInfoService.deleteEcardModuleInfoByIds(ids));
    }

    /**
     * 电子名片模块功能状态修改
     * @param ecardModuleInfo
     * @return
     */
    @RequiresPermissions("ecard:config:module:edit")
    @Log(title = "电子名片模块功能状态修改", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(EcardModuleInfo ecardModuleInfo) {
        return toAjax(ecardModuleInfoService.updateEcardModuleInfo(ecardModuleInfo));
    }
}
